package util

import (
	"strconv"
	"strings"

	"mall/common/enumx"
	"mall/common/globalkey"
	"mall/service/forum/rpc/forumclient"
)

const (
	arrayValueSeparator    = ","
	keyValueSeparator      = ":"
	keyOpSeparator         = ","
	arrayKeyValueSeparator = ";"
)

// todo: 重构
func ParseSorter(sorter string) (res []*forumclient.BackendOrderBy) { //////
	sorters := strings.Split(sorter, arrayKeyValueSeparator)
	if len(sorters) > 0 {
		for _, orderBy := range sorters {
			orderBys := strings.SplitN(orderBy, keyValueSeparator, 2)
			if len(orderBys) == 2 && orderBys[0] != "" && orderBys[1] != "" {
				res = append(res, &forumclient.BackendOrderBy{
					Field: orderBys[0],
					Order: orderBys[1],
				})
			}
		}
	}

	return
}

func ParseFilter(filter string, postType int32) (res []*forumclient.BackendFilterBy) { //////
	filters := strings.Split(filter, arrayKeyValueSeparator)
	if len(filters) > 0 {
		for _, filterBy := range filters {
			filterBys := strings.SplitN(filterBy, keyValueSeparator, 2)
			if len(filterBys) == 2 && filterBys[0] != "" && filterBys[1] != "" {
				res = append(res, &forumclient.BackendFilterBy{
					Field: filterBys[0],
					In:    filterBys[1],
				})
			}
		}
	}
	res = append(res, &forumclient.BackendFilterBy{
		Field: "type",
		In:    strconv.FormatInt(int64(postType), 10),
	})

	return
}

func ParseSorterNew(sorter *string) (res []*forumclient.OrderBy) { // ##24.11.14##
	if sorter == nil {
		return
	}

	sorters := strings.Split(*sorter, arrayKeyValueSeparator)
	if len(sorters) == 0 {
		return
	}

	for _, orderBy := range sorters {
		orderBys := strings.Split(orderBy, keyValueSeparator)

		if len(orderBys) == 2 && len(orderBys[0]) > 0 {
			ascOrDesc := "0"
			if len(orderBys[1]) > 0 {
				ascOrDesc = orderBys[1]
			}
			res = append(res, &forumclient.OrderBy{
				Field: orderBys[0],
				Order: ascOrDesc,
			})
			continue
		}

		if len(orderBys) == 1 && len(orderBys[0]) > 0 {
			res = append(res, &forumclient.OrderBy{
				Field: orderBys[0],
				Order: "0",
			})
		}
	}

	return
}

func ParseFilterNew(filter *string, postType *uint32) (res []*forumclient.FilterBy) { // ##24.11.14##
	if filter != nil {
		filters := strings.Split(*filter, arrayKeyValueSeparator)
		if len(filters) > 0 {
			for _, filterBy := range filters {
				filterBys := strings.Split(filterBy, keyValueSeparator)

				if len(filterBys) == 2 && len(filterBys[0]) > 0 && len(filterBys[1]) > 0 {
					res = append(res, &forumclient.FilterBy{
						Field: filterBys[0],
						In:    filterBys[1],
					})
				}
			}
		}
	}

	if postType != nil {
		res = append(res, &forumclient.FilterBy{
			Field: "type",
			In:    strconv.FormatInt(int64(*postType), 10),
		})
	}

	return
}

func GetStatusText(status int32) (res string) { /////
	switch enumx.ObjectStatus(status) {
	case enumx.ObjectStatusPending:
		res = "待审核"
	case enumx.ObjectStatusCheckSuccess:
		res = "建议通过"
	case enumx.ObjectStatusCheckFailed:
		res = "建议驳回"
	case enumx.ObjectStatusAproved:
		res = "已通过"
	case enumx.ObjectStatusRejected:
		res = "已驳回"
	case enumx.ObjectStatusDeleted:
		res = "已删除"
	default:
		res = ""
	}

	return
}

type Option struct {
	CanAccept      bool
	CanReject      bool
	CanStick       bool
	CanUnstick     bool
	CanRemove      bool
	CanRestore     bool
	CanForceDelete bool
	ShowGallery    bool
}

func GetOptionByStatusAndType(status *uint32, sticky *uint32, postType *uint64) *Option {
	res := &Option{}

	if postType != nil && status != nil && *postType == uint64(globalkey.ForumPostTypePhoto) {
		switch int32(*status) {
		case globalkey.StatusPending, globalkey.StatusCheckSuccess:
			res.ShowGallery = true
		default:
			res.ShowGallery = false
		}

		return res
	}

	if status != nil {
		switch int32(*status) {
		case globalkey.StatusPending, globalkey.StatusCheckSuccess:
			res.CanAccept = true
			res.CanReject = true
		case globalkey.StatusCheckFailed:
			res.CanReject = true
		case globalkey.StatusApproved:
			if sticky != nil && *sticky > 0 {
				res.CanUnstick = true
			} else {
				res.CanStick = true
			}
		case globalkey.StatusDeleted:
			res.CanForceDelete = true
		}
	}

	return res
}
